package com.wjf.accesscontrol.ui.setting

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.wjf.accesscontrol.base.MVVMBaseFragment
import com.wjf.accesscontrol.databinding.FragmentActivateBinding
import com.wjf.accesscontrol.entity.ActivateEntity
import com.wjf.accesscontrol.entity.StationDetailEntity
import com.wjf.accesscontrol.net.utils.Status
import com.wjf.accesscontrol.ui.read.dialog.DIALOG_TYPE_4
import com.wjf.accesscontrol.ui.read.dialog.ResultDialogCallback
import com.wjf.accesscontrol.ui.setting.adapter.ActivateAdapter
import com.wjf.accesscontrol.util.AppUtils
import com.wjf.accesscontrol.util.COMMON_FLAG_DATA
import com.wjf.accesscontrol.util.COMMON_FLAG_MSG
import com.wjf.accesscontrol.util.Constant
import com.wjf.accesscontrol.util.SPUtils
import com.wjf.accesscontrol.util.SP_KEY_MACHINE_ID
import com.wjf.accesscontrol.util.ToastUtils
import com.wjf.accesscontrol.util.singleClick

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2024/2/1 16:43
 *
 */

class ActivateFragment : MVVMBaseFragment<SettingViewModel, FragmentActivateBinding>(), ResultDialogCallback {

    companion object{
        fun newInstance(msg: String): ActivateFragment{
            val activateFragment = ActivateFragment()
            val bundle = Bundle()
            bundle.putString(COMMON_FLAG_MSG,msg)
            activateFragment.arguments = bundle
            return activateFragment
        }
    }

    private var requestState = false
    private var activateState = ""
    private var activateMachineId = ""

    override fun initViewModel() = ViewModelProviders.of(this).get(SettingViewModel::class.java)

    override fun initViewBinding(inflater: LayoutInflater, container: ViewGroup?) =
        FragmentActivateBinding.inflate(inflater,container,false)

    override fun initView() {
        val linearLayoutManager = LinearLayoutManager(this.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rvActivate.layoutManager = linearLayoutManager
    }

    override fun initData() {
        super.initData()
        activateMachineId = "${arguments?.getString(COMMON_FLAG_MSG)}"
        vm.stationDetail(activateMachineId,Constant.baseUrl).observe(this, Observer {
            it?.let { resource ->
                when(resource.status){
                    Status.SUCCESS -> {
                        requestState = true
                        if (resource.data is StationDetailEntity){
                            activateState = "${resource.data.machineStatus}"
                            binding.tvLevel.text = resource.data.stationLevelName
                            val dates =listOf(
                                ActivateEntity("管理机构：", resource.data.orgName ?: ""),
                                ActivateEntity("负责人：",resource.data.chargePerson ?: ""),
                                ActivateEntity("联系方式：",resource.data.chargePhone ?: ""),
                                ActivateEntity("站点地址：",resource.data.addr ?: ""),
                                ActivateEntity("站点介绍：",resource.data.introduction ?: "")
                            )
                            binding.rvActivate.adapter = ActivateAdapter(dates)

                        }
                    }

                    Status.ERROR -> {
                        ToastUtils.show(resource.message)
                    }
                }
            }
        })
    }

    override fun initClick() {

        binding.tvReturn.singleClick {
            activity?.finish()
        }

        binding.tvCancel.singleClick {
            activity?.finish()
        }

        binding.tvConfirm.singleClick {
            if (!requestState){
                ToastUtils.show("数据异常")
                return@singleClick
            }

            if (activateState != "4"){
                ToastUtils.show("设备已经激活，不能再次激活")
                return@singleClick
            }

            vm.deviceMachine(activateMachineId,Constant.baseUrl).observe(this, Observer {
                when(it.status){

                    Status.SUCCESS -> {
                        Constant.machineId = activateMachineId
                        SPUtils.instance.commit(SP_KEY_MACHINE_ID,activateMachineId)
                        AppUtils.showResultDialog(
                            fragmentManager = childFragmentManager,
                            resultDialogCallback = this,
                            type = DIALOG_TYPE_4,
                            msg = "即将重启App"
                        )
                    }

                    Status.ERROR -> {
                        ToastUtils.show(it.message)
                    }

                }
            })
        }
    }

    override fun dismiss() {
        AppUtils.restartApp()
    }
}